package hn.cch.microservice.service_consumer_metadata.controller;


import hn.cch.microservice.service_consumer_metadata.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

//加载配置文件路径
@PropertySource(value = {"classpath:application.properties"})
@RestController
public class UserController {

    //读取配置文件参数
    @Value(value = "${rest.user.url}")
    private String restUserUrl;

    @Autowired
    private DiscoveryClient discoveryClient;

    @Autowired
    private RestTemplate restTemplate;

    @Bean
    public RestTemplate restTemplate() {
        RestTemplate restTemplate = new RestTemplate();
        return restTemplate;
    }

    @GetMapping(value = "/{id}")
    public User user(@PathVariable Long id) {
        return restTemplate.getForObject(restUserUrl + id, User.class);
    }

    /**
     * 获取服务实例信息
     * @return
     */
    @GetMapping(value = "get-instance")
    public List<ServiceInstance> getInstance(){
        return discoveryClient.getInstances("service_provider_metadata");
    }

}
